package com.shycloud.mido.homework.thread;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

import com.shycloud.mido.common.api.constants.HomeWorkConstants;
import com.shycloud.mido.common.api.enums.HomeworkStatusEnum;
import com.shycloud.mido.common.core.util.SpringContextHolder;
import com.shycloud.mido.homework.service.MidoHomeworkMainService;

import lombok.extern.slf4j.Slf4j;
/**
 * 线程处理
 * @author bard.bai
 *
 */
@Slf4j
public class HomeworkStatusThread implements Runnable {

	private LocalDate startdate;
    private String studentId;
    private String isParentMode;
    private boolean isStudent;
    private String curTeacherIdByStu;
    private Map<LocalDate, String> resultMap;
    private Semaphore semaphore;
    private CountDownLatch countDownLatch;

    public HomeworkStatusThread(LocalDate startdate,String studentId,String isParentMode,boolean isStudent,String curTeacherIdByStu,Map<LocalDate, String> resultMap, Semaphore semaphore,
            CountDownLatch countDownLatch) {
    	this.startdate = startdate;
        this.studentId = studentId;
        this.isParentMode = isParentMode;
        this.isStudent = isStudent;
        this.curTeacherIdByStu = curTeacherIdByStu;
        this.resultMap = resultMap;
        this.semaphore = semaphore;
        this.countDownLatch = countDownLatch;
    }


    @Override
    public void run() {
        try {
            semaphore.acquire();
            MidoHomeworkMainService midoHomeworkMainService = SpringContextHolder.getBean(MidoHomeworkMainService.class);
           
        	//查询当天作业完成情况
			List<String> statusList = new ArrayList<>();
			//查询当天是否有被提醒
			List<String> dingString = new ArrayList<>();

			//目标
			if ("1".equals(isParentMode)) {
				//家长模式作业
				statusList.addAll(midoHomeworkMainService.getHomeWorkStatus(startdate, studentId, HomeWorkConstants.PARENT_MODE_TEACHER_ID));
				dingString.addAll(midoHomeworkMainService.getHomeWorkDing(startdate, studentId, HomeWorkConstants.PARENT_MODE_TEACHER_ID));
			} else {
				if (isStudent) {
					statusList.addAll(midoHomeworkMainService.getHomeWorkStatusByStu(startdate, studentId));
					dingString.addAll(midoHomeworkMainService.getHomeWorkDingByStu(startdate, studentId));
				} else {
					statusList.addAll(midoHomeworkMainService.getHomeWorkStatus(startdate, studentId, curTeacherIdByStu));
					dingString.addAll(midoHomeworkMainService.getHomeWorkDing(startdate, studentId, curTeacherIdByStu));
				}
			}
			

			Boolean dingFlg = false;

			if (dingString.contains("1")) {
				dingFlg = true;
			}

			int allNO = 0;
			int allOk = 0;

			//没有作业
			if (statusList.size() == 0 || statusList == null) {
				resultMap.put(startdate, HomeworkStatusEnum.NO_HOMEWORK.getCode());
				return;
			}

			//循环判断状态
			for (String status : statusList) {
				//作业状态(0-未开始,1-未完成,2-已完成)
				if (HomeWorkConstants.NOT_STARTED.equals(status) || HomeWorkConstants.UNFINISHED.equals(status)) {
					allNO++;
				}

				if (HomeWorkConstants.COMPELTED.equals(status)) {
					allOk++;
				}
			}

			//全没完成
			if (allNO == statusList.size()) {
				resultMap.put(startdate, HomeworkStatusEnum.ALL_UNFINISHED.getCode());
			}

			//全没完成并且提醒了
			if (allNO == statusList.size() && dingFlg) {
				resultMap.put(startdate, HomeworkStatusEnum.SUCCESS.getCode());
			}

			//全部完成
			if (allOk == statusList.size()) {
				resultMap.put(startdate, HomeworkStatusEnum.ALL_FINISHED.getCode());
			}

			//一半一半
			if (allNO != statusList.size() && allOk != statusList.size()) {
				resultMap.put(startdate, HomeworkStatusEnum.HALF_FINISHED.getCode());
			}

			//一半一半 并且提醒了
			if (allNO != statusList.size() && allOk != statusList.size() && dingFlg) {
				resultMap.put(startdate, HomeworkStatusEnum.HALF_FINISHED_REMINDED.getCode());
			}
          
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            log.debug(Thread.currentThread().getName() + "，日期："
                    + startdate+"，学生" + studentId);
            semaphore.release();
            countDownLatch.countDown();
        }

    }
    
    

}